/**
 * SpringMVC restful 风格
 * 用户控制器，控制关于用户的一些业务逻辑操作
 * @author yanwl
 * @date 2014-10-30
 */
package com.chnbst.bstyun.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.Pager;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.vo.UserVo;
import com.chnbst.bstyun.service.UserService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author lq
 *
 */
@Controller
@RequestMapping("/userAdmin")
public class UserAdminController {
	//每页显示的用户数
	private int pageSize = 10;
	//用户列表页码
	private int currPage = 1;
	
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private UserService userService;
	
	/**
	 * @return 个人用户列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/userList",method=RequestMethod.GET)
	public ModelAndView userList()throws Exception{
		return new ModelAndView("admin/userManager");
	}
	
	/**
	 * @return 企业列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/companyList",method=RequestMethod.GET)
	public ModelAndView companyList()throws Exception{
		return new ModelAndView("admin/companyManager");
	}
	
	/**
	 * Ajax 分页查询个人用户list
	 * @return 个人用户列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/userList/search/",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userSearch(String page)throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		
		int totalRows = userService.selectUserCountByIsYun("1")+userService.selectUserCountByIsYun("2");
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		
		map.put("start", start);
		map.put("pageSize", Integer.MAX_VALUE);
		map.put("sortField", "user_id");
		map.put("sortOrder", "desc");
		
		List<User> userList = userService.selectUsersByIsYun("1");
		List<User> puyList = userService.selectUsersByIsYun("2");
		userList.addAll(puyList);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("userList", userList);
		
		return resultMap;
	}
	
	/**
	 * 根据ID查询个人用户单条数据的详细信息
	 * @return 个人用户详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/userDetail/{userId}",method=RequestMethod.GET)
	public ModelAndView paymentDetail(@PathVariable Integer userId)throws Exception{
		
		ModelAndView mav = new ModelAndView();
		
		//加载用户的详细信息
		UserVo userInfo = userService.selectUserVoByUserId(userId);
		
		mav.addObject("userInfo", userInfo);
		mav.setViewName("admin/userDetail");
		
		return mav;
	}
	
	/**
	 * Ajax 分页查询企业用户list
	 * @return 企业用户列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/companyList/search/",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> companySearch(String page)throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		int totalRows = userService.selectUserCountByIsYun("0");
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		
		map.put("start", start);
		map.put("pageSize", Integer.MAX_VALUE);
		map.put("sortField", "user_id");
		map.put("sortOrder", "desc");
		List<User> companyList = userService.selectUsersByIsYun("0");
		//List<User> companyList = userService.selectUsers(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("companyList", companyList);
		
		return resultMap;
	}
	
	/**
	 * 根据ID查询单条企业用户数据的详细信息
	 * @return 企业用户详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/companyDetail/{userId}",method=RequestMethod.GET)
	public ModelAndView companyDetail(@PathVariable Integer userId)throws Exception{
		
		ModelAndView mav = new ModelAndView();
		
		UserVo userInfo = userService.selectUserVoByUserId(userId);
		
		mav.addObject("userInfo", userInfo);
		mav.setViewName("admin/companyDetail");
		
		return mav;
	}
}
